home *** CD-ROM | disk | FTP | other *** search
/ Ray Dream Studio 5 / Ray Dream.iso / pc / DreamSDK / Macintosh / Samples / link / LINK / COMLINK.cpp next >
Encoding:
Text File  |  1997-07-09  |  4.8 KB  |  202 lines

  1. // Copyright (c)1995 Ray Dream, Inc. All Rights Reserved.
  2. /* $Id: COMLINK.cpp 1.4 1997/04/05 02:39:22 damien Exp $ */
  3.  
  4. ////////////////////////////////////////////////////////////////////////
  5. //  Motion Link Example                                               //
  6. //--------------------------------------------------------------------//
  7. //   Implementation of the ScrewLink Interface                        //
  8. //////////////////////////////////////////////////////////////////////// 
  9.  
  10. #include "math.h"
  11.  
  12. #ifndef __COMLINK__
  13. #include "COMLink.h"
  14. #endif
  15.  
  16. #ifndef __LINKDLL__
  17. #include "LinkDLL.h"
  18. #endif
  19.  
  20. #ifndef __3DCOFAIL__
  21. #include "3DCoFail.h"
  22. #endif
  23.  
  24. const NUM3D kTwoPi = 3.14159265358979323846 * 2.0;
  25.  
  26. #undef INTERFACE
  27. #define INTERFACE ScrewLink
  28. // Constructor / Destructor of the C++ Object :
  29. ScrewLink::ScrewLink() {
  30.   fCRef=0; // Reference Counter
  31.   // Data initialisation :
  32.   fData.fAxis = kAxisZ;
  33.   fData.fStep = 1.0;
  34.   fData.fFreedomValue = 0.0;
  35.   }
  36.   
  37. ScrewLink::~ScrewLink() {
  38.   global_count_Obj--; 
  39.   }
  40.   
  41. // IUnknown Interface :
  42. HRESULT ScrewLink::QueryInterface(THIS_ REFIID riid,LPVOID* ppvObj) {
  43.   *ppvObj=NULL;
  44.   
  45.   // The ScrewLink knows the interfaces of the parent Objects
  46.   if (IsEqualIID(riid, IID_IUnknown))
  47.     *ppvObj=(LPVOID)this;
  48.   else if (IsEqualIID(riid, IID_I3DExMotionLink))
  49.     *ppvObj=(LPVOID)this;
  50.   else if (IsEqualIID(riid, IID_I3DExDataExchanger))
  51.     *ppvObj=(LPVOID)this;
  52.   else if (IsEqualIID(riid, IID_I3DExtension))
  53.     *ppvObj=(LPVOID)this;
  54.     
  55.   // we must add reference if we return an interface
  56.   if (*ppvObj!=NULL) {
  57.     ((LPUNKNOWN)*ppvObj)->AddRef();
  58.     return NOERROR;
  59.     }
  60.   else {
  61.     return ResultFromScode(E_NOINTERFACE);
  62.     }
  63.   }
  64.  
  65. ULONG ScrewLink::AddRef(THIS) {
  66.   return fCRef++;
  67.   }
  68.   
  69. ULONG ScrewLink::Release(THIS) {
  70.   ULONG UnreleaseObject=fCRef--;
  71.   
  72.   if (fCRef==0)
  73.      delete this; // No reference left, so destroy the object
  74.   
  75.   return UnreleaseObject;
  76.   // local variable used, because fCRef can be destroyed before.
  77.   }
  78.   
  79. // I3DExtension methods :
  80. I3DExtension* ScrewLink::Clone(THIS) {
  81.   ScrewLink* theClone = new ScrewLink;
  82.   if (theClone) {
  83.     theClone->AddRef();
  84.     theClone->fData=fData; // copy the ScrewLinkData
  85.     }                               
  86.   return theClone;
  87.   }   
  88.  
  89. HRESULT ScrewLink::ShellUtilitiesInit(THIS_ IShUtilities* shellUtilities) {
  90.   InitCoFailure(shellUtilities);
  91.   return NOERROR;
  92.   }
  93.  
  94. // I3DExDataExchanger methods :
  95. ExtensionDataMap* ScrewLink::GetExtensionDataMap(THIS) {
  96.   return NULL;
  97.   }
  98.  
  99. void* ScrewLink::GetExtensionDataBuffer(THIS) {
  100.   return &fData; // used by the shell to set the new parameters
  101.   }
  102.   
  103. HRESULT ScrewLink::ExtensionDataChanged(THIS) {
  104.   return NOERROR;
  105.   }
  106.  
  107. HRESULT ScrewLink::HandleEvent(THIS_ ULONG SourceID) {
  108.   return ResultFromScode(E_NOTIMPL);
  109.   }
  110.  
  111. short ScrewLink::GetResID(THIS) {
  112.   return 141; // this is the view ID in the resource file.
  113.   }
  114.   
  115. // I3DExMotionLink methods :
  116. short ScrewLink::GetNbrFreedom(THIS) {
  117.   return 1;
  118.   }
  119.  
  120. HRESULT ScrewLink::GetFreedomRange(THIS_ short index, NUM3D* min, NUM3D* max) {
  121.   if (index==1) {
  122.     *min = -32767.0;
  123.     *max =  32767.0;
  124.     }
  125.   return NOERROR;
  126.   }
  127.  
  128. HRESULT ScrewLink::IncrementFreedomValue(THIS_ short index, NUM3D* increment) {
  129.   if (index==1) { 
  130.     fData.fFreedomValue += *increment;
  131.     }
  132.   return NOERROR;
  133.   }
  134.  
  135.  
  136.  
  137. HRESULT ScrewLink::GetTransform(THIS_ TRANSFORM3D* transform) {
  138.   short u,v,w;
  139.   NUM3D alpha,cosa,sina;
  140.   NUM3D altitude;
  141.   NUM3D m33[3][3];
  142.  
  143.   altitude = fData.fFreedomValue * fData.fStep;
  144.   alpha = fData.fFreedomValue * kTwoPi;
  145.  
  146.   if (fData.fAxis==kAxisX) {
  147.     u=1;v=2;w=0;
  148.     }
  149.   else if (fData.fAxis==kAxisY) {
  150.     u=2;v=0;w=1;
  151.     }
  152.   else if (fData.fAxis==kAxisZ) {
  153.     u=0;v=1;w=2;
  154.     } 
  155.   transform->fT[u] = 0.0;
  156.   transform->fT[v] = 0.0;
  157.   transform->fT[w] = altitude;
  158.   sina=sin(alpha);
  159.     cosa=cos(alpha); //QuickSinCos(alpha,sina,cosa);
  160.   m33[u][u]=cosa; m33[u][v]=sina; m33[u][w]=0.0;
  161.   m33[v][u]=-sina; m33[v][v]=cosa; m33[v][w]=0.0;
  162.   m33[w][u]=0.0; m33[w][v]=0.0; m33[w][w]=1.0;
  163.   transform->fR = *(MATRIX3D*)&m33;
  164.                         
  165.   return NOERROR;
  166.   }
  167.  
  168. HRESULT ScrewLink::GetTransformPartialDerivate(THIS_ short index, TRANSFORM3D* transform) {
  169.   short u,v,w;
  170.   NUM3D alpha,cosa,sina;
  171.   NUM3D m33[3][3];
  172.   if (fData.fAxis==kAxisX) {
  173.     u=1;v=2;w=0;
  174.     }
  175.   else if (fData.fAxis==kAxisY) {
  176.     u=2;v=0;w=1;
  177.     }
  178.   else if (fData.fAxis==kAxisZ) {
  179.     u=0;v=1;w=2;
  180.     } 
  181.   if (index==1) {
  182.     transform->fT[u] = 0.0;
  183.     transform->fT[v] = 0.0;
  184.     transform->fT[w] = fData.fStep;
  185.     alpha = kTwoPi * fData.fFreedomValue;
  186.         sina=sin(alpha);
  187.         cosa=cos(alpha); //QuickSinCos(alpha,sina,cosa);
  188.     m33[u][u]=-kTwoPi * sina;
  189.         m33[u][v]=kTwoPi * cosa;
  190.         m33[u][w]=0.0;
  191.     m33[v][u]=-kTwoPi * cosa;
  192.         m33[v][v]=-kTwoPi * sina;
  193.         m33[v][w]=0.0;
  194.     m33[w][u]=0.0;
  195.         m33[w][v]=0.0;
  196.         m33[w][w]=0.0;
  197.     transform->fR = *(MATRIX3D*)&m33;
  198.                          
  199.     }
  200.   return NOERROR;
  201.   }
  202.